package com.qq.BFMRSE.frame;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import com.qq.BFMRSE.encrypt.desEncrypt;
import com.qq.BFMRSE.requestModule.decFileModule;

public class decFrame extends JFrame implements ActionListener{
	private JLabel encFileLabel;
	private JLabel keyPathLabel;
	private JTextField encFileTF;
	private JTextField keyPathTF;
	private JButton decBtn ;
	private JButton encFileChoseBtn;
	private JButton keyPathchoseBtn;
	private JFileChooser jfc;
	private JTextField decFileTF;
	private JLabel decFileLabel;
	private JButton decFileBtn;

	/**
	 * Create the frame.
	 */
	public decFrame() {
		init();
		this.setTitle("解密界面");
		getContentPane().setLayout(null);
		jfc.setCurrentDirectory(new File("d://"));// 文件选择器的初始目录定为d盘
		keyPathchoseBtn.addActionListener(this);
		decBtn.addActionListener(this);
		setSize(500, 300);
		setVisible(true);
		setResizable(true);// 禁止拉边框拉长拉断
		setLocationRelativeTo(null);
        ImageIcon imgBF=new ImageIcon("images\\BFMRSE.png");
        this.setIconImage(imgBF.getImage()); 
	}
	public void init(){
		encFileLabel = new JLabel("加密文件选择：");
		keyPathLabel= new JLabel("秘钥存储目录：");
		encFileTF = new JTextField(10);
		keyPathTF= new JTextField(10);
		decBtn = new JButton("开始解密并保存");
		decBtn.addActionListener(this);
		encFileLabel.setBounds(79, 40, 114, 30);
		getContentPane().add(encFileLabel);
		keyPathLabel.setBounds(79, 122, 114, 30);
		getContentPane().add(keyPathLabel);
		encFileTF.setBounds(189, 43, 100, 25);
		getContentPane().add(encFileTF);
		keyPathTF.setBounds(189, 125, 100, 25);
		getContentPane().add(keyPathTF);
		decBtn.setBounds(79, 172, 299, 30);
		getContentPane().add(decBtn);
		jfc = new JFileChooser();// 文件选择器
		keyPathchoseBtn = new JButton("选择");
		keyPathchoseBtn.setBounds(314, 126, 64, 25);
		getContentPane().add(keyPathchoseBtn);
		keyPathchoseBtn.addActionListener(this);
		
		encFileChoseBtn = new JButton("选择");
		encFileChoseBtn.setBounds(314, 44, 64, 25);
		getContentPane().add(encFileChoseBtn);
		encFileChoseBtn.addActionListener(this);
		decFileLabel = new JLabel("解密文件目录：");
		decFileLabel.setBounds(78, 82, 100, 30);
		getContentPane().add(decFileLabel);
		decFileTF = new JTextField();
		decFileTF.setBounds(189, 87, 100, 25);
		getContentPane().add(decFileTF);
		decFileTF.setColumns(10);
		decFileBtn = new JButton("选择");
		decFileBtn.setBounds(314, 86, 64, 25);
		getContentPane().add(decFileBtn);
		decFileBtn.addActionListener(this);
	}
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					decFrame frame = new decFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource()==encFileChoseBtn){
			jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
			int state = jfc.showOpenDialog(null);// 此句是打开文件选择器界面的触发语句
			if (state == 1) {
				return;// 撤销则返回
			} else {
				File skFile=jfc.getSelectedFile();
				encFileTF.setText(skFile.getAbsolutePath());
			}
		}else if(e.getSource()==decFileBtn){
			jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			int state = jfc.showOpenDialog(null);// 此句是打开文件选择器界面的触发语句
			if (state == 1) {
				return;// 撤销则返回
			} else {
				File skFile=jfc.getSelectedFile();
				decFileTF.setText(skFile.getAbsolutePath());
			}
		}else if(e.getSource()==keyPathchoseBtn){
			jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			int state = jfc.showOpenDialog(null);// 此句是打开文件选择器界面的触发语句
			if (state == 1) {
				return;// 撤销则返回
			} else {
				File skFile=jfc.getSelectedFile();
				keyPathTF.setText(skFile.getAbsolutePath());
			}
		}else if(e.getSource()==decBtn){
			//开始进行解密操作
			String encFilePath=encFileTF.getText();
			String decFilePath=decFileTF.getText();
			String keyFilePath=keyPathTF.getText();
			if("".equals(encFilePath)||"".equals(decFilePath)||"".equals(keyFilePath)){
				JOptionPane.showMessageDialog(this, "信息选择不完整，请进行选择！",
						"提示",JOptionPane.INFORMATION_MESSAGE);
			}else{
				desEncrypt dEn=new desEncrypt(keyFilePath);
				decFileModule deModule=new decFileModule(dEn);
				  try {
					deModule.decFiles(encFilePath, decFilePath);
				} catch (Exception ex) {
					// TODO Auto-generated catch block
					ex.printStackTrace();
				}
				  JOptionPane.showMessageDialog(this, "文件解密成功！",
							"提示",JOptionPane.INFORMATION_MESSAGE);
			}
		}
	}
}
